"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _jsxRuntime = require("react/jsx-runtime");

var _icons = require("@ant-design/icons");

var _antdV = require("antd-v4");

var _react = require("react");

var _reactSortableHoc = require("react-sortable-hoc");

var _Table = _interopRequireDefault(require("../../Table"));

var _useDragSort2 = require("../../utils/useDragSort");

require("./index.less");

var _excluded = ["rowKey", "dragSortKey", "dragSortHandlerRender", "onDragSortEnd", "onDataSourceChange", "columns", "dataSource"];

// 用于创建可拖拽把手组件的工厂
var handleCreator = function handleCreator(handle) {
  return (0, _reactSortableHoc.SortableHandle)(function () {
    return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
      children: handle
    });
  });
};

function DragSortTable(props) {
  var rowKey = props.rowKey,
      dragSortKey = props.dragSortKey,
      dragSortHandlerRender = props.dragSortHandlerRender,
      onDragSortEnd = props.onDragSortEnd,
      onDataSourceChange = props.onDataSourceChange,
      propsColumns = props.columns,
      oriDs = props.dataSource,
      otherProps = (0, _objectWithoutProperties2.default)(props, _excluded);

  var _useContext = (0, _react.useContext)(_antdV.ConfigProvider.ConfigContext),
      getPrefixCls = _useContext.getPrefixCls; // 默认拖拽把手


  var DragHandle = (0, _react.useMemo)(function () {
    return handleCreator((0, _jsxRuntime.jsx)(_icons.MenuOutlined, {
      className: getPrefixCls('pro-table-drag-icon')
    }));
  }, [getPrefixCls]);
  var isDragSortColumn = (0, _react.useCallback)(function (item) {
    return item.key === dragSortKey || item.dataIndex === dragSortKey;
  }, [dragSortKey]); // 根据 dragSortKey 查找目标列配置

  var handleColumn = (0, _react.useMemo)(function () {
    return propsColumns === null || propsColumns === void 0 ? void 0 : propsColumns.find(function (item) {
      return isDragSortColumn(item);
    });
  }, [propsColumns, isDragSortColumn]); // 记录原始列配置

  var originColumnRef = (0, _react.useRef)((0, _objectSpread2.default)({}, handleColumn)); // 使用自定义hooks获取拖拽相关组件的components集合

  var _useDragSort = (0, _useDragSort2.useDragSort)({
    dataSource: oriDs === null || oriDs === void 0 ? void 0 : oriDs.slice(),
    dragSortKey: dragSortKey,
    onDragSortEnd: onDragSortEnd,
    components: props.components,
    rowKey: rowKey
  }),
      components = _useDragSort.components; // 重写列配置的render


  var columns = (0, _react.useMemo)(function () {
    var originColumn = originColumnRef.current;
    if (!handleColumn) return propsColumns;

    var dargRender = function dargRender() {
      var _originColumn$render;

      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      var dom = args[0],
          rowData = args[1],
          index = args[2],
          action = args[3],
          schema = args[4];
      var RealHandle = dragSortHandlerRender ? handleCreator(dragSortHandlerRender(rowData, index)) : DragHandle;
      return (0, _jsxRuntime.jsx)("div", {
        className: getPrefixCls('pro-table-drag-visible-cell'),
        children: (0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
          children: [(0, _jsxRuntime.jsx)(RealHandle, {}), (_originColumn$render = originColumn.render) === null || _originColumn$render === void 0 ? void 0 : _originColumn$render.call(originColumn, dom, rowData, index, action, schema)]
        })
      });
    }; // 重新生成数据


    return propsColumns === null || propsColumns === void 0 ? void 0 : propsColumns.map(function (item) {
      if (!isDragSortColumn(item)) {
        return item;
      }

      return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, item), {}, {
        render: dargRender
      });
    });
  }, [DragHandle, dragSortHandlerRender, getPrefixCls, handleColumn, isDragSortColumn, propsColumns]);
  return handleColumn ? (0, _jsxRuntime.jsx)(_Table.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, otherProps), {}, {
    rowKey: rowKey,
    dataSource: oriDs,
    components: components,
    columns: columns,
    onDataSourceChange: onDataSourceChange
  })) :
  /* istanbul ignore next */
  (0, _jsxRuntime.jsx)(_Table.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, otherProps), {}, {
    rowKey: rowKey,
    dataSource: oriDs,
    columns: columns,
    onDataSourceChange: onDataSourceChange
  }));
}

var _default = DragSortTable;
exports.default = _default;